package com.example.cushion.realize.alarm;

import android.app.KeyguardManager;
import android.app.WallpaperManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.example.cushion.R;
import com.example.cushion.base.BaseActivity;

import org.itzheng.and.baseutils.common.ArrayUtils;
import org.itzheng.and.baseutils.log.LogHelper;
import org.itzheng.and.baseutils.ui.UIUtils;
import org.itzheng.utils.LocationUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Title:<br>
 * Description: <br>
 *
 * @email ItZheng@ZoHo.com
 * Created by itzheng on 2018-5-2.
 */
public class AlarmActivity extends BaseActivity {
    private static final String TAG = "AlarmActivity";
    List<View> mList;

    public void initViewList() {
        if (ArrayUtils.isEmpty(mList)) {
            mList = new ArrayList<>();
            mList.add(new View(this));
            View view = UIUtils.inflate(R.layout.fragment_alarm_slide_close);
            view.findViewById(R.id.tvClose).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UIUtils.showToast("close");
                }
            });
            mList.add(view);
        }
    }

    @Override
    public void onBackPressed() {

    }

    View lockView = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        LogHelper.d(TAG, "onCreate: ");
        super.onCreate(savedInstanceState);
        setTranslucentStatus(true);
        setTranslucentNavigation(true);
//        getWindow().getDecorView().setBackgroundColor(0x7fff0000);
        setContentView(R.layout.layout_view_pager);
        testLocation();
//        lockView = UIUtils.inflate(R.layout.layout_view_pager);
        setWallPaper();
        setViewPager();
//        setLockView();
//        isLockScreen();
    }

    private void testLocation() {

    }

    private void isLockScreen() {
        final KeyguardManager mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    boolean flag = mKeyguardManager.inKeyguardRestrictedInputMode();
                    LogHelper.d(TAG, "是否锁屏:" + flag);
                }
            }
        }).start();


    }

    WindowManager windowManager;

    private void setLockView() {
        windowManager = (WindowManager) this
                .getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                        | WindowManager.LayoutParams.FLAG_FULLSCREEN, PixelFormat.TRANSPARENT);
        layoutParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
//        layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
        lockView.setFocusable(true);
        lockView.setFocusableInTouchMode(true);
        windowManager.addView(lockView, layoutParams);
    }

    private void setViewPager() {
        initViewList();
        ViewPager vpViewPager = findViewById(R.id.vpViewPager);
        vpViewPager.setAdapter(new SlideAdapter());
        vpViewPager.setOverScrollMode(ViewPager.OVER_SCROLL_NEVER);
        vpViewPager.setCurrentItem(1);
        vpViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    if (windowManager != null) {
                        windowManager.removeView(lockView);
                    }
                    finish();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private class SlideAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return ArrayUtils.size(mList);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mList.get(position));
            return mList.get(position);
        }
    }

    public void setWallPaper() {
        ImageView ivWallpaper = findViewById(R.id.ivWallpaper);
        // 获取壁纸管理器
        WallpaperManager wallpaperManager = WallpaperManager

                .getInstance(this);

        // 获取当前壁纸
        Drawable wallpaperDrawable = wallpaperManager.getDrawable();
        // 将Drawable,转成Bitmap
        final Bitmap bm = ((BitmapDrawable) wallpaperDrawable).getBitmap();
        ivWallpaper.setScaleType(ImageView.ScaleType.CENTER_CROP);
        ivWallpaper.setImageBitmap(bm);
    }
}
